package com.bruce.algorithm.leetcode;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * @author luotuan
 * @Description 5，最长回文字串
 * @create 2020-07-14 10:29
 **/
public class Palindrome {
    private int max = 0;
    private String result = "";

    public String longestPalindrome(String s) {
        if (s.length() == 1) {
            return s;
        }

        for (int i = 0; i < s.length() - 1; i++) {
            check(s, i, i);
            check(s, i, i + 1);
        }

        return result;
    }

    public void check(String s, int l, int h) {
        while (l >= 0 && h < s.length()) {
            if (s.charAt(l) == s.charAt(h)) {
                int size = h - l + 1;
                if (size > max) {
                    max = size;
                    result = s.substring(l, h + 1);
                }
                l--;
                h++;
            } else {
                return;
            }
        }
    }

    public static void main(String[] args) {
        String s = "ac";
        System.out.println(new Palindrome().longestPalindrome(s));
    }
}
